package org.mog.music.controller;

import cn.hutool.json.JSONUtil;
import dto.MusicInsertDTO;
import dto.MusicPageDTO;
import dto.MusicSearchDTO;
import entity.pojo.Music;
import entity.resullt.PageResult;
import entity.resullt.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.mog.music.mapper.MusicMapper;
import org.mog.music.service.IMusicService;
import org.springframework.web.bind.annotation.*;
import vo.MusicPageVO;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Api(tags = "离线音乐相关接口")
@RestController
@RequestMapping("/user/offline/music")
@RequiredArgsConstructor
@Slf4j
public class OfflineMusicController {
    private final IMusicService musicService;

    /**
     * 音乐分页查询
     *
     * @param musicPageDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("音乐分页查询")
    public Result<PageResult<MusicPageVO>> pageMusic(MusicPageDTO musicPageDTO) {
        log.info("音乐分页查询：{}", musicPageDTO);
        PageResult<MusicPageVO> musicPage = musicService.getMusicPage(musicPageDTO);
        log.info("查询成功：{}", musicPage);
        return Result.success(musicPage);
    }

    /**
     * 新增音乐
     *
     * @param musicInsertDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增音乐")
    public Result insertMusic(@RequestBody MusicInsertDTO musicInsertDTO, HttpServletRequest request) {
        log.info("新增音乐：{}", musicInsertDTO);
        musicService.insertMusic(musicInsertDTO, request, null);
        log.info("新增成功");
        return Result.success();
    }

    /**
     * 增加播放次数
     *
     * @param id
     * @return
     */
    @ApiOperation("增加播放次数")
    @PutMapping("/addPlays")
    public Result addPlays(Long id) {
        log.info("增加播放次数，{}", id);
        Result result = musicService.addPlays(id);
        log.info("操作成功");
        return result;
    }
}
